package com.woniuxy.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dto.EmployerDto;
import com.woniuxy.entity.Employee;
import com.woniuxy.service.EmployerService;
import com.woniuxy.util.Result;
import com.woniuxy.vo.EmployerVo;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class EmployerController {

    @NonNull
    private EmployerService employerService;


    @GetMapping("selctAll")
    public  Result selectAll(){
        List<Employee> employees = employerService.selectAll();
        return  Result.ok().data("employees",employees);
    }

    @GetMapping("test01")
    public  Result test01(){
        return  Result.ok();
    }


    @PostMapping("insert")
    public  Result insert(@RequestBody Employee employee){
        employerService.insert(employee);
        return  Result.ok();
    }

    @GetMapping("selectById/{id}")
    public Result selectEmployerById(@PathVariable Long id){
        Employee employee = employerService.selectById(id);
        return Result.ok().data("employee",employee);
    }

    @PutMapping("update")
    public  Result update(@RequestBody Employee employee){
        employerService.update(employee);
        return  Result.ok();
    }

    @DeleteMapping("deleteEmployerById/{id}")
    public  Result delete(@PathVariable Long id){
        employerService.deleteEmployerById(id);
        return  Result.ok();
    }

    @PostMapping("select")
    public Result selectByLike(@RequestBody EmployerDto employerDto){
        PageHelper.startPage(employerDto.getCurrentPage(),employerDto.getPageSize(),true);
        List<EmployerVo> employers = employerService.selectByLike(employerDto);
        PageInfo<EmployerVo> pageInfo = new PageInfo<>(employers);
        return Result.ok().data("empList",employers).data("total",pageInfo.getTotal());
    }
}
